{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="HOME_CSS/site.css"/>
{/block}
{block name="main"}
<div class="ns-store-create">
	<div class="layui-form ns-store-add">
		<span class="ns-card-title">创建店铺</span>
		
		<div class="ns-input-block layui-form-item">
			<label class="ns-input-title"><span class="required">*</span>店铺名称：</label>
			<div class="ns-input-store">
				<input name="site_name" type="text" lay-verify="required" class="layui-input" autocomplete="off">
			</div>
		</div>
		
		<!--<div class="ns-input-block layui-form-item">-->
			<!--<label class="ns-input-title">域名设置：</label>-->
			<!--<div class="ns-input-store">-->
				<!--<input name="site_domain" type="text" class="layui-input" autocomplete="off">-->
			<!--</div>-->
		<!--</div>-->

		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">创建店铺</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
	
	<div class="ns-create-box ns-progress-box layui-hide">
		<img src="SHOP_IMG/apply/apply_succeed.png" />
		<div class="layui-progress layui-progress-big" id="processBar" lay-filter="demo">
			<div class="layui-progress-bar ns-bg-color" lay-percent="0"></div>
		</div>
		<p>创建店铺中...</p>
	</div>
	
	<div class="ns-create-box ns-success-box layui-hide">
		<div class="ns-success-img ns-bg-color">
			<i class="iconfont iconduihao1"></i>
		</div>
		<p>店铺创建成功，<a class="ns-text-color" href="">进入店铺后台（<span>3</span>）</a></p>
	</div>
	
	<div class="ns-create-box ns-fail-box layui-hide">
		<div class="ns-fail-img">
			<i class="iconfont iconclose_light"></i>
		</div>
		<p>店铺创建失败，<a class="ns-text-color" onclick="reAdd()">重新创建</a></p>
	</div>
</div>
{/block}
{block name="script"}
<script>
	var promotion_items = $("#promotion a").length,
		extend_items = $("#admin a").length,
		interaction_items = $("#member a").length,
		tool_item = $("#tool").length;
	if (promotion_items == 0) {
		$("#promotion").hide();
	}
	if (extend_items == 0) {
		$("#admin").hide();
	}
	if (interaction_items == 0) {
		$("#member").hide();
	}
	if (tool_item == 0) {
		$("#tool").hide();
	}
	layui.use(['form', 'upload'], function() {
		var form = layui.form,
			element = layui.element,
			$ = layui.jquery,
			repeat_flag = false; //防重复标识
		form.render();

		/**
		 * 监听提交
		 */
		form.on('submit(save)', function(data) {
			$(".ns-progress-box").removeClass("layui-hide");
			$(".ns-progress-box").siblings().addClass("layui-hide");
			
			var n = 0;
			var countDown = 4;
			element.progress('demo', n+'%');
			
			var timer = setInterval(function(){
				
				n = n + Math.random()*10|0;  
				if(n>100){
					n = 100;
					clearInterval(timer);
				}
				element.progress('demo', n+'%');
				if (repeat_flag) return false;
				repeat_flag = true;
				//添加
				$.ajax({
					type: 'POST',
					url: ns.url("home/site/add"),
					data: data.field,
					dataType: 'JSON',
					success: function(res) {
						repeat_flag = false;
						
						if (res.code == 0) {
							n = 100;
							element.progress('demo', n+'%');
							clearInterval(timer);
							setTimeout(function(){
								$(".ns-success-box").removeClass("layui-hide");
								$(".ns-success-box").siblings().addClass("layui-hide");
							}, 1000);

							//自动跳转店铺
							setTimeout(function(){
								location.href = $(".ns-success-box a").attr("href");
							}, 3000);

							//倒计时显示
							var countDownTimer = setInterval(function () {
								if (countDown == 1)clearInterval(countDownTimer);
								countDown--;
								$(".ns-success-box a span").text(countDown);
							},1000);

							$(".ns-success-box a").attr("href", ns.url('shop/index/index', {site_id: res.data}));
						} else {
							clearInterval(timer);
							setTimeout(function(){
								$(".ns-fail-box").removeClass("layui-hide");
								$(".ns-fail-box").siblings().addClass("layui-hide");
							}, 1000);
							layer.msg(res.message);
						}
					}
				});
			}, 100);
		});
	});
	
	/**
	 * 点击搜索
	 */
	var repeat_flag = false;
	var html, val;

	function checkMember() {
		var parent = $(".ns-check-member");
		var con = parent.find(".ns-member-name").val();
		$(".layui-word-aux").remove();
		$(".ns-search-result").remove();

		if (repeat_flag) return false;
		repeat_flag = true;

		if (con == "" || con == null || con.trim() == "") {
			repeat_flag = false;
		} else {
			$.ajax({
				type: 'POST',
				url: ns.url("admin/member/searchMember"),
				data: {
					'search_text': con
				},
				dataType: 'JSON',
				success: function(res) {
					layer.msg(res.message);
					repeat_flag = false;

					if (res.data == null) {
						html = '<span class="layui-word-aux">未找到该用户</span>';
						val = res.data;
					} else {
						html = '<div class="ns-search-result layui-input-inline ns-border-color-gray">' +
							'<div class="ns-search-res-img">' +
							'<img src="' + ns.img(res.data.headimg) + '" />' +
							'</div>' +
							'<div class="ns-search-res-intro">' +
							'<p>用户名：' + res.data.username + '</p>' +
							'<p>电话：' + res.data.mobile + '</p>' +
							'</div>' +
							'<div class="ns-search-res-close" onclick="closeMember()">' +
							'<i class="iconfont iconclose_light"></i>' +
							'</div>' +
							'</div>';
						val = res.data.member_id;
					}

					$(".ns-member-id").attr("value", val);
					$(".ns-check-member").append(html);
				}
			});
		}
	}

	function closeMember() {
		$(".ns-search-result").hide();
	}
	
	function reAdd() {
		$(".ns-store-add").removeClass("layui-hide");
		$(".ns-store-add").siblings().addClass("layui-hide");
	}
	
	function back() {
		location.href = ns.url("home/index/index");
	}
</script>
{/block}